#!/bin/bash
VERSION=1.0

if [ $# -lt 3 ]; then
  echo "GNAP overlay tool $VERSION"
  echo "Usage:"
  echo "	$0 [-t iso] gnap_core overlay_root isooutputfile"
  echo "        $0 -t disk gnap_core overlay_root hostdev targetdev [cache]"
  echo
  echo "Options:"
  echo "	gnap_core	Path to the GNAP core tarball"
  echo "	overlay_root	Path to the directory containing the overlay"
  echo
  echo "Options for \"iso\" output (default):"
  echo "	isooutputfile	Customized LiveCD ISO output file"
  echo
  echo "Options for \"disk\" output:"
  echo "        hostdev         Device to install to, as seen on current host"
  echo "        targetdev       Boot device name as seen on target environment"
  echo
  echo "Example:"
  echo "	$0 gnap-core-$VERSION.tar overlays/firewall/ myfirewall.iso"
  echo "        $0 -t disk mycore.tar overlays/test/ /dev/hdc1 hda1"
  exit
fi

TYPE="iso"
if [ "$1" == "-t" ]; then
  shift
  TYPE=$1
  shift
fi

GNAPCORE=$1
shift
OVERLAYDIR=$1
shift
OUTPUT=$1
shift
  
if [ "$TYPE" == "disk" ]; then
  PARENTDEV=${OUTPUT:0:$((${#OUTPUT}-1))}
  if [ ! -b $PARENTDEV ]; then
    echo "Parent device $PARENTDEV for $OUTPUT does not exist on this system"
    exit 1
  fi
  if [ ! -b $OUTPUT ]; then
    echo "$OUTPUT partition does not exist on this system"
    exit 1
  fi
  echo "Warning : you have selected disk install"
  echo "Make sure you are root or have full access to $OUTPUT"
  echo "$PARENTDEV must have an MBR installed"
  echo "  run 'dd if=mbr/mbr.bin of=$PARENTDEV bs=512 count=1' if needed"
  echo "$OUTPUT must contain an active partition"
  echo "  use 'fdisk $PARENTDEV' if needed"
  echo
  read -ep "Are your sure you want to continue [N]: " ANSWER
  if [ "$ANSWER" != "y" -a "$ANSWER" != "Y" ]; then
    exit 0
  fi
fi

if [ ! -e $OVERLAYDIR/etc/overlay.conf ]; then
  echo Please use a valid overlay dir -- should contain etc/overlay.conf
  exit
fi
if [ ! -e $GNAPCORE ]; then
  echo Please use a valid gnap_core
  exit
fi

echo Expanding core...
TEMPCOREDIR=`mktemp -d -t gnapcore_XXXXXXXXXX`
if [ $? != 0 ]; then
  echo "Failed to make tmpfile"
  exit 1
fi
tar x -C $TEMPCOREDIR -f $GNAPCORE

echo Overlaying files ...
tar zc -C $OVERLAYDIR -f $TEMPCOREDIR/overlay.tgz .

DATE=`date --utc`

echo "Overlaid with directory $OVERLAYDIR on $DATE" \
     >> $TEMPCOREDIR/isolinux/boot.msg

if [ "$TYPE" == "iso" ]; then

  rm $TEMPCOREDIR/syslinux.cfg
  echo Building $OUTPUT ...
  mkisofs -quiet -J -r -l -o $OUTPUT \
        -b isolinux/isolinux.bin -c isolinux/boot.cat \
        -no-emul-boot -boot-load-size 4 -boot-info-table $TEMPCOREDIR

else
  if [ "$TYPE" == "disk" ]; then
    TARGETDEV=$1
    shift
    CACHE=""
    if [ $# -eq 1 ]; then
      if [ "$1" == "cache" ]; then
        CACHE="docache"
      fi
    fi
    TEMPMOUNTDIR=`mktemp -d -t gnapmount_XXXXXX`
    if [ $? != 0 ]; then
       echo "Failed to make tmpdir"
       exit 1
    fi
    echo Formatting and Mounting...
    mkfs.msdos $OUTPUT
    mount -t msdos -o sync $OUTPUT $TEMPMOUNTDIR
    if [ $? != 0 ]; then
      echo Failed to mount $OUTPUT
    fi
    echo Copying files...
    cp $TEMPCOREDIR/syslinux.cfg          $TEMPMOUNTDIR/
    cp $TEMPCOREDIR/isolinux/gentoo.igz   $TEMPMOUNTDIR/
    cp $TEMPCOREDIR/isolinux/gentoo       $TEMPMOUNTDIR/
    cp $TEMPCOREDIR/isolinux/*.msg        $TEMPMOUNTDIR/
    cp $TEMPCOREDIR/isolinux/memtest86    $TEMPMOUNTDIR/memtest
    cp $TEMPCOREDIR/overlay.tgz           $TEMPMOUNTDIR/
    cp $TEMPCOREDIR/livecd.squashfs       $TEMPMOUNTDIR/livecd.sfs

    sed -i "s/cdroot/cdroot=\/dev\/$TARGETDEV $CACHE console=ttyS0,19200n81/" $TEMPMOUNTDIR/syslinux.cfg
    
    umount $TEMPMOUNTDIR
    rm -rf $TEMPMOUNTDIR

    echo Syslinuxing...
    export MTOOLS_SKIP_CHECK=1
    syslinux $OUTPUT

  else
    echo !! Unknown output type : $TYPE
  fi
fi

echo Cleanup ...
rm -rf $TEMPCOREDIR

echo Done.
